<?php

namespace UploadFile\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use UploadFile\Form\SingleUpload;

class ImagesController extends AbstractActionController
{
    protected $imagineService;

    public function indexAction()
    {
        $formManager = $this->serviceLocator->get('FormElementManager');
        $form = $formManager->get('UploadFile\Form\SingleUpload');

        //$form = new SingleUpload('file-form');

        if ($this->getRequest()->isPost()) {
            // Postback
            $data = array_merge_recursive(
                $this->getRequest()->getPost()->toArray(),
                $this->getRequest()->getFiles()->toArray()
            );

            $form->setData($data);
            if ($form->isValid()) {
                $data = $form->getData();

                $uploadPath     =  $data['file']['tmp_name'];//'data/uploads/';

                $imagine = $this->getImagineService();
                $size = new \Imagine\Image\Box(2916, 2083);
                $mode = \Imagine\Image\ImageInterface::THUMBNAIL_INSET;

                $image = $imagine->open($uploadPath);

                //$image->thumbnail($size, $mode)->save($uploadPath);
                $image->save($uploadPath , array('jpg_compression_level' => 9));

                die(print '123');
                $adapter        = new \Zend\File\Transfer\Adapter\Http();
                $adapter->setDestination($uploadPath);

                $adapter->setValidators(array(
                    new \Zend\Validator\File\Extension(array(
                            'extension' => array('jpg', 'jpeg', 'png', 'rtf')
                        )
                    ),
                ));

                if (!$adapter->isValid()) {
                //    throw new \Exception(json_encode("!isValid " . implode(" ", $adapter->getMessages())));
                }

                $files = $adapter->getFileInfo();

                foreach ($files as $file => $info) {
                    $name = $adapter->getFileName($file);

                    // file uploaded & is valid
                    if (!$adapter->isUploaded($file)) {
                        //throw new \Exception(json_encode("!isUploaded") . implode(" ", $adapter->getMessages()));
                        //continue;
                    }

                    if (!$adapter->isValid($file)) {
                        //throw new \Exception(json_encode("!isValid " . implode(" ", $adapter->getMessages())));
                        //continue;
                    }

                    // receive the files into the user directory
                    $check = $adapter->receive($file); // this has to be on top

                    if (!$check) {
                        //throw new \Exception(json_encode("! receive" . implode(" ", $adapter->getMessages())));
                    }

                    $fileclass             = new \stdClass();
                    // we stripped out the image thumbnail for our purpose, primarily for security reasons
                    // you could add it back in here.
                    $fileclass->name       = $name;
                    $fileclass->size       = $adapter->getFileSize($name);
                    $fileclass->type       = $adapter->getMimeType($name);
                    //$fileclass->deleteUrl  = '.data/uploads/delete';
                    //$fileclass->deleteType = 'DELETE';
                    //$fileclass->error = 'null';
                    //$fileclass->url        = '/';
                    die(print_r($fileclass));

                    $datas['files'][] = $fileclass;
                }

                //return $this->redirectToSuccessPage($form->getData());
            }
        }

        return new ViewModel(array('form' => $form));
    }

    public function getImagineService()
    {
        if ($this->imagineService === null)
        {
            $this->imagineService = $this->getServiceLocator()->get('upload_imagine_image_service');
        }
        return $this->imagineService;
    }

}

